import { getCollection } from "astro:content";
import { execSync } from "node:child_process";

function getCommitCount() {
  const command = 'git rev-list --count HEAD';
  try {
    const output = execSync(command);
    const commitCount = parseInt(output.toString().trim(), 10);
    return commitCount;
  } catch (error: any) {
    console.error(`获取commit次数失败: ${error.toString()}`);
    return 0;
  }
}

function getTagCount() {
  const command = 'git tag --list | wc -l';
  try {
    const output = execSync(command);
    const tagCount = parseInt(output.toString().trim(), 10);
    return tagCount;
  } catch (error: any) {
    console.error(`获取tag数量失败: ${error.toString()}`);
    return 0;
  }
}

export default async function useWikiInfo() {
  const wikiItem = await getCollection("wiki", ({ data }) => !data.draft);
  return {
    /** 词条数目 */
    itemCount: wikiItem.length,
    /** git 提交次数 */
    commitCount: getCommitCount(),
    /** git tag 数目 */
    tagCount: getTagCount(),
  }
}